package cn.com.dhcc.ddi.datasource.dao;

import java.util.List;

import cn.com.dhcc.app.core.dao.BaseDao;
import cn.com.dhcc.app.core.dao.SimpleDaoForOracle;
import cn.com.dhcc.app.core.exception.DaoException;
import cn.com.dhcc.app.core.exception.NiceException;
import cn.com.dhcc.app.core.handler.modulehandler.WhereCondition;
import cn.com.dhcc.app.core.handler.pagerhandler.Pager;
import cn.com.dhcc.app.core.handler.pagerhandler.PagerData;
import cn.com.dhcc.app.core.util.JsonUtil;
import cn.com.dhcc.app.pub.CM;
import cn.com.dhcc.ddi.datasource.vo.DataSourceVo;
import cn.com.dhcc.ddi.datasource.vo.wapper.DataSourceRowWapper;

import org.springframework.stereotype.Repository;
/**
 * 
 * DataSourceDao
 * @Author: fxx
 * @date: 2017-5-16
 *
 */
@Repository
public class DataSourceDao extends BaseDao {
	
	/**
	 * 初始化数据源
	 * @param dataSource
	 * @return
	 * @throws NiceException  getDaoByPublishDataSource
	 */
	public SimpleDaoForOracle  initDao(DataSourceVo dataSource) throws DaoException{
		final SimpleDaoForOracle dao = new SimpleDaoForOracle();
		try {
			dao.initDataSource(dataSource.getDataSourceConfVo().getDriverName(), dataSource.getDataSourceConfVo().getUrl(), dataSource.getDataSourceConfVo().getUserName(), dataSource.getDataSourceConfVo().getPassword());
		} catch (Exception e) {
			throw new DaoException("初始化数据源失败", e);
		}
		return dao;
	}
	/**
	 * 获取对象
	 * @param id
	 * @return
	 * @throws DaoException
	 */
	public DataSourceVo get(String id) throws DaoException {
		return getDao().getModuleById("获取对象", DataSourceVo.class, new Object[]{id});
	}
	
	/**
	 * 
	 * @Title: getPagerList
	 * @Description: 获取数据源列表
	 * @param @param pager
	 * @param @param searchParameter
	 * @param @param id
	 * @param @return
	 * @param @throws DaoException 参数
	 * @return PagerData<DataSourceVo> 返回类型
	 * @throws
	 */
	public PagerData<DataSourceVo> getPagerList(Pager pager, WhereCondition searchParameter) throws DaoException {
		String sql = "select t1.id,t1.node_id,t2.name node_name,t1.name,t1.type,t1.conf,t1.status,t1.mtime,t1.remark from "
				    +    SI_TABLE("t_ddi_datasource")+" t1 "
				    +" left join "
				    +    SI_TABLE("t_ddi_node_info")+" t2 on t1.node_id=t2.id ";
		return getDao().getPagerList("获取数据源列表", sql, new DataSourceRowWapper(), pager, searchParameter, new Object[]{});
	}
	
	/**
	 * 获取全部数据源列表
	 * @return 全部数据源列表
	 * @throws DaoException
	 */
	public List<DataSourceVo> getDsList() throws DaoException {
		String sql = "SELECT * FROM " + SI_TABLE("t_ddi_datasource") + " WHERE STATUS='1'";
		return getDao().list("获取全部数据源", sql, new DataSourceRowWapper(), new Object[]{});
	}
	
	/**
	 * 获取当前用户全部数据源列表
	 * @return 全部数据源列表
	 * @throws DaoException
	 */
	public List<DataSourceVo> getDsList(WhereCondition searchParameter) throws DaoException {
		String sql = "SELECT * FROM " + SI_TABLE("t_ddi_datasource") + " WHERE STATUS='1'";
		return getDao().list("获取全部数据源", sql, new DataSourceRowWapper(), searchParameter, new Object[]{});
	}
	
	/**
	 * 获取对象
	 * @param id
	 * @return
	 * @throws DaoException
	 */
	public DataSourceVo getById(String id) throws DaoException {
		String sql = "select t1.id,t1.node_id,t2.name node_name,t1.name,t1.type,t1.conf,t1.status,t1.mtime,t1.remark from "
			    +    SI_TABLE("t_ddi_datasource")+" t1 "
			    +" left join "
			    +    SI_TABLE("t_ddi_node_info")+" t2 on t1.node_id=t2.id where t1.id=? ";
		return getDao().get("根据id获取数据源", sql, new DataSourceRowWapper(), new Object[]{id});
	}

	/**
	 * 新增
	 * @param obj
	 * @return
	 * @throws DaoException
	 */
	public int insert(DataSourceVo obj) throws DaoException {
		return getDao().insertModule("新增", obj);
	}

	/**
	 * 修改
	 * @param obj
	 * @return
	 * @throws DaoException
	 */
	public int update(DataSourceVo obj) throws DaoException {
		return getDao().updateModuleByIdExecuteNull("修改", obj);
	}
	/**
	 * 
	 * @Title: updateDataSource
	 * @Description: 数据源基本信息修改
	 * @param @param vo
	 * @param @return
	 * @param @throws DaoException    参数
	 * @return int    返回类型
	 * @throws
	 */
	public int updateDataSource(DataSourceVo vo) throws DaoException {
		String sql = "update " + SI_TABLE("t_ddi_datasource")+" set node_id=?, name=?, type=?, conf=?, status=?, mtime=?, remark=? where id=? ";
		String conf = JsonUtil.toJson(vo.getDataSourceConfVo());
		return CM.getDao().update("数据源基本信息修改",sql, new Object[] { vo.getNodeId(),
						        vo.getName(), vo.getType(), conf, vo.getStatus(), vo.getMtime(), vo.getRemark(), vo.getId()});
		
	}

	/**
	 * 新增或修改
	 * @param obj
	 * @return
	 * @throws DaoException
	 */
	public int insertOrUpdate(DataSourceVo obj) throws DaoException {
		return getDao().saveOrUpdateModuleByIdExecuteNull("新增或修改", obj);
	}
	
	/**
	 * 
	 * @Title: insertDataSource
	 * @Description: 数据源基本信息入库
	 * @param @param vo
	 * @param @throws DaoException 参数
	 * @return void 返回类型
	 * @throws
	 */
	public int insertDataSource(DataSourceVo vo) throws DaoException {
		String sql = "insert into " + SI_TABLE("t_ddi_datasource")+"(id,node_id,name,type,conf,status,mtime,remark) values(?,?,?,?,?,?,?,?)";
		String conf = JsonUtil.toJson(vo.getDataSourceConfVo());
		return CM.getDao().insert("数据源基本信息入库", sql, new Object[] { vo.getId(), vo.getNodeId(),
						          vo.getName(), vo.getType(), conf, vo.getStatus(), vo.getMtime(), vo.getRemark() });
	}
	
	/**
	 * 
	 * @Title: delete
	 * @Description: 删除
	 * @param @param id
	 * @param @return
	 * @param @throws DaoException 参数
	 * @return int 返回类型
	 * @throws
	 */
	public int delete(String id) throws DaoException {
		return getDao().deleteModuleById("", DataSourceVo.class, new Object[]{id});
	}
	
    /**
     * 验证名称唯一性
        * @Title: count
        * @Description: TODO(这里用一句话描述这个方法的作用)
        * @param @param searchParameter
        * @param @return
        * @param @throws DaoException    参数
        * @return int    返回类型
        * @throws
     */
    public int count(WhereCondition searchParameter) throws DaoException {
		String sql = "select count(*) from " + SI_TABLE("t_ddi_datasource");
		return CM.getDao().queryForInt("数据源信息已存在", sql, searchParameter, new Object[]{});
	}

	/**
	 * 获取ds id
	 * @param searchParameter
	 * @return
	 * @throws DaoException
     */
	public List<String> getDsid(WhereCondition searchParameter) throws DaoException {
		String sql = "select id from " + SI_TABLE("t_ddi_datasource");
		return CM.getDao().listSignleStringColumn("获取dsid", sql, searchParameter, new Object[]{});
	}
}
